package com.imooc.controller;

import com.imooc.entity.bo.User;
import com.imooc.valid.UserValidator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Map;
import java.util.Set;


@Controller
@RequestMapping("/valid")
public class ValidatorController {
    @Autowired
    private UserValidator validator;

    @InitBinder
    private void initBinder(WebDataBinder binder) {
        binder.addValidators(validator);
    }

    @RequestMapping(value={"/index", "/", ""},method= {RequestMethod.GET})
    public String index(ModelMap m) throws Exception {
        m.addAttribute("user", new User());
       return "user.jsp";
    }

    @RequestMapping(value={"/signup"},method= {RequestMethod.POST})
    public String signup(@Validated User user, BindingResult br, RedirectAttributes ra) throws Exception {
        ra.addFlashAttribute("user", user);
        return " user.jsp ";
    }

	//执行处理前给Model设置("className ", " com.excelib.controller.GoController ")
	@ModelAttribute("className")
	public String setModel() throws IOException {
		return this.getClass().getName();
	}
	//执行处理前给Model设置("teacher", "孔老夫子")
	@ModelAttribute
	public void setModel1(Model model) throws IOException {
		model.addAttribute("teacher", "孔老夫子");
	}
	//执行处理前给Model设置("string", "excelib")
	@ModelAttribute
	public String setModel2() throws IOException {
		return "excelib";
	}
	@RequestMapping(value={"/index","/"},method= {RequestMethod.GET})
	public String index(HttpServletRequest request, Model model) throws Exception {
		Map<String,Object> m = model.asMap();
		System.out.println("className"+"——>"+m.get("className"));
		System.out.println("teacher"+"——>"+m.get("teacher"));
		System.out.println("string"+"——>"+m.get("string"));
		model.addAttribute("msg", "Go Go Go!");
		return "go.jsp";
	}

	@RequestMapping(value={"/signup1"},method= {RequestMethod.POST})
	public String signup1( @Validated User user, BindingResult br, RedirectAttributes ra) throws Exception {
		ra.addFlashAttribute("user", user);
		return "redirect:show";
	}

	@RequestMapping(value={"/show"},method= {RequestMethod.GET})
	public String show (@Validated User user, BindingResult br) throws Exception {
		return "/user.jsp";
	}


	// Get  /students/176
	@RequestMapping(value={"/students/{studentId}"},method= {RequestMethod.GET})
	public void getStudent(@PathVariable String studentId){
		// studentId = 176
	}
	// Get  /students/176/books/7
	@RequestMapping(value={"/students/{studentId}/books/{bookId}"},method= {RequestMethod.GET})
	public void getStudentBook(@PathVariable String bookId, @PathVariable String studentId){
		// studentId = 176, bookId = 7
	}
	// Get  /books/7
	@RequestMapping(value={"/books/{id}"},method= {RequestMethod.GET})
	public void getBook(@PathVariable("id") String bookId){
		// bookId = 7
	}

	// Get  /telephoneNumbers/liyang;gender=male
	@RequestMapping(value={"/telephoneNumbers7/{name}"},method= {RequestMethod.GET})
	public void getTel(@PathVariable String name, @MatrixVariable String gender){
		// name=liyang  gender=male
	}

	// Get /telephoneNumbers/liyang;gender=male;group=englishTeacher
	@RequestMapping(value={"/telephoneNumbers6/{name}"},method= {RequestMethod.GET})
	public void getTel(@PathVariable String name, @MatrixVariable String gender, @MatrixVariable String group){
		// name=liyang  gender=male  group=englishTeacher
	}

	// Get /telephoneNumbers/liyang;group=englishTeacher
	@RequestMapping(value={"/telephoneNumbers1/{name}"},method= {RequestMethod.GET})
	public void getTel1(@PathVariable String name, @MatrixVariable(required = false) String gender, @MatrixVariable(required = false) String group){
		// name=liyang  gender=null  group=englishTeacher
	}

	// Get /telephones2/iphone/telephoneNumbers/liyang;group=englishTeacher
	@RequestMapping(value={"/telephones2/{telName}/telephoneNumbers/{name}"},method= {RequestMethod.GET})
	public void getTel(@PathVariable String telName, @PathVariable String name,
					   @MatrixVariable(required = false) String gender,
					   @MatrixVariable(required = false) String group){
		// telName=iphone
		// name=liyang  gender=null  group=englishTeacher
	}

	/*如果设置的属性是一个set集合*/
	// Get  /telephones3/xiaomi;colors=black,red,golden/telephoneNumbers/liyang;group=englishTeacher
	@RequestMapping(value={"/telephones3/{telName}/telephoneNumbers/{name}"},method= {RequestMethod.GET})
	public void getTel(@PathVariable String telName, @PathVariable String name, @MatrixVariable(required = false) Set<String> colors, @MatrixVariable(required = false) String gender, @MatrixVariable(required = false) String group){
		// telName=xiaomi  colors={black,red,golden}
		// name=liyang  gender=null  group=englishTeacher
	}

	/*这里设置了两个group*/
	// Get /telephones4/xiaomi;colors=black,red,golden;group=note/telephoneNumbers/liyang;group=englishTeacher
	@RequestMapping(value={"/telephones4/{telName}/telephoneNumbers/{name}"},method= {RequestMethod.GET})
	public void getTel(@PathVariable String telName, @MatrixVariable(value="group", pathVar="telName", required = false) String telGroup,@PathVariable String name, @MatrixVariable(required = false) Set<String> colors, @MatrixVariable(required = false) String gender, @MatrixVariable(value="group", pathVar="name", required = false) String group){
		// telName=xiaomi  colors={black,red,golden} telGroup=note
		// name=liyang  gender=null  group=englishTeacher
	}


	/*两个group 通过map的方式进行搜集*/
	// Get /telephones5/xiaomi;colors=black,red,golden;group=note/telephoneNumbers/liyang;gender=male;group=englishTeacher
	@RequestMapping(value={"/telephones5/{telName}/telephoneNumbers/{name}"},method= {RequestMethod.GET})
	public void getTel(@MatrixVariable Map<String, String> matrixVars,
					   @MatrixVariable(pathVar="telName") Map<String, String> telMatrixVars,
					   @MatrixVariable(pathVar="name") Map<String, String> contactMatrixVars){
		// matrixVars: {colors=[black, red, golden], group=[note, englishTeacher], gender=[male]}
		// telMatrixVars: {colors=[black, red, golden], group=[note]}
		// contactMatrixVars: {gender=[male], group=[englishTeacher]}
	}




}
